package practice;

import java.util.*;

public class _建物流中转站_ {
    public static void main(String[] args) {
        _建物流中转站_ c = new _建物流中转站_();
        _建物流中转站_.Solution s = c.getSolutin();
        Scanner scanner = new Scanner(System.in);
        int M = Integer.valueOf(scanner.nextLine());
        String[] arr = new String[M];
        Integer[][] loc = new Integer[M][M];
        int temp = M;
        while (temp > 0 ){
            arr[M-(temp--)] = scanner.nextLine();
        }
        for (int i = 0; i < M; i++) {
            String[] line = arr[i].split(" ");
            for (int j = 0; j < M; j++) {
                loc[i][j] = Integer.valueOf(line[j]);
            }
        }
        s.where(loc);
    }


    Solution getSolutin() {
        return new Solution();
    }

    class Solution {
        public void where(Integer[][] arr ){
//            int[] x_pos = new int[];
//            int[] y_pos = new int[];
            List<Integer> x_pos = new ArrayList();
            List<Integer> y_pos = new ArrayList();
            int count = 0;
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[i].length; j++) {
                    if (arr[i][j] == 1) {
                        x_pos.add(i);
                        y_pos.add(j);
                        count++;
                    }
                }
            }

            if (count == arr.length*arr.length) {
                System.out.println(-1);
                System.exit(0);
            }
            int cost = Integer.MAX_VALUE;
            int temp = 0;
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length; j++) {
                    if (arr[i][j] == 0) {
                        for (int k = 0; k < count; k++) {
                            temp += (Math.abs(x_pos.get(k)-i) + Math.abs(y_pos.get(k)-j));
                        }
                        if (temp < cost) cost = temp;
                    }
                    temp = 0;
                }
            }
            System.out.println(cost);
        }
    }
}
